import { $http } from '@/utils';
import { $ResponseCode } from '@/utils/const/env';
import { $message } from '../message';
import { makePhoneCall } from '../functions';
export const AppointmentService = {
    user: () => {
        return new Promise((resolve, reject) => {
            $http.get('AppointmentService/user').then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res.data);
                } else {
                    reject(res);
                }
            }).catch(() => {
                reject({ msg: '服务器繁忙，请稍后再试' });
            })
        })
    },
    technician: () => {
        return new Promise((resolve, reject) => {
            $http.get('AppointmentService/technician').then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res.data);
                } else {
                    reject(res);
                }
            }).catch(() => {
                reject({ msg: '服务器繁忙，请稍后再试' });
            })
        })
    },
    details: (query: any) => {
        return new Promise((resolve, reject) => {
            $http.get('AppointmentService/details', {
                data: query
            }).then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res.data);
                } else {
                    reject(res);
                }
            }).catch(() => {
                reject({ msg: '服务器繁忙，请稍后再试' });
            })
        })
    },
    verification(data: any) {
        return new Promise((resolve, reject) => {
            $http.post('AppointmentService/verification', {
                data
            }).then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res);
                } else {
                    reject(res);
                }
            }).catch(() => {
                reject({ msg: '服务器繁忙，请稍后再试' });
            })
        })
    },
    departure(data: any) {
        return new Promise((resolve, reject) => {
            $http.post('AppointmentService/departure', {
                data
            }).then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res);
                } else {
                    reject(res);
                }
            }).catch(() => {
                reject({ msg: '服务器繁忙，请稍后再试' });
            })
        })
    },
    arrival(data: any) {
        return new Promise((resolve, reject) => {
            $http.post('AppointmentService/arrival', {
                data
            }).then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res);
                } else {
                    reject(res);
                }
            }).catch(() => {
                reject({ msg: '服务器繁忙，请稍后再试' });
            })
        })
    },
    startService(data: any) {
        return new Promise((resolve, reject) => {
            $http.post('AppointmentService/startService', {
                data
            }).then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res);
                } else {
                    reject(res);
                }
            }).catch(() => {
                reject({ msg: '服务器繁忙，请稍后再试' });
            })
        })
    },
    endService(data: any) {
        return new Promise((resolve, reject) => {
            $http.post('AppointmentService/endService', {
                data
            }).then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res);
                } else {
                    reject(res);
                }
            }).catch(() => {
                reject({ msg: '服务器繁忙，请稍后再试' });
            })
        })
    },
    getTechnicianAppointmentService(query: any) {
        return new Promise((resolve, reject) => {
            $http.get('AppointmentService/getTechnicianAppointmentService', {
                data: query
            }).then((res: any) => {
                if (res.code === $ResponseCode.SUCCESS) {
                    resolve(res.data);
                } else if (res.code === $ResponseCode.PAY_SUCCESS) {
                    resolve(res);
                } else {
                    reject(res);
                }
            }).catch(() => {
                reject({ msg: '服务器繁忙，请稍后再试' });
            });
        });
    },
    makePhoneCall(data: any) {
        uni.showLoading({
            title: '获取中...',
            mask: true
        })
        $http.post('AppointmentService/makePhoneCall', {
            data
        }).then((res: any) => {
            uni.hideLoading();
            if (res.code === $ResponseCode.SUCCESS) {
                const mobile = res.data.mobile;
                uni.showActionSheet({
                    title: '电话：' + mobile,
                    itemList:['拨打电话', '复制电话号码'],
                    success: (res) => {
                        if (res.tapIndex === 0) {
                            makePhoneCall(mobile);
                        } else if (res.tapIndex === 1) {
                            uni.setClipboardData({
                                data: mobile,
                                success: () => {
                                    $message.success('已复制');
                                }
                            });
                        }
                    }
                })
            } else {
                $message.info(res.msg);
            }
        }).catch(() => {
            uni.hideLoading();
            $message.info('服务器繁忙，请稍后再试');
        });
    }
}